package Jdom;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


/**
 * created by zhangyuhang on 2018/7/17
 */
public class ReSetSVG {


    public static Document load(File file) {
        Document document = null;
        try {
            SAXReader saxReader = new SAXReader();
            document = saxReader.read(file); // 读取XML文件,获得document对象
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return document;
    }


    public static void main(String args[]){

        File file = new File("/Users/zhangyuhang/Desktop/wk/seleniumDemo/seleniumtest/src/main/resources/abrupt_3.svg");
        Document document=ReSetSVG.load(file);
        Element root = document.getRootElement();
        Attribute widthAttribute = root.attribute("width");
        Attribute heightAttribute = root.attribute("height");

        if (widthAttribute ==null||heightAttribute ==null){
            String viewBoxValue = root.attribute("viewBox").getValue();
            String[] viewBoxValueArray = viewBoxValue.split(" ");
            System.out.println(viewBoxValueArray[2]+viewBoxValueArray[3]);
            root.addAttribute("width",viewBoxValueArray[2]);
            root.addAttribute("height",viewBoxValueArray[3]);
            OutputFormat format = OutputFormat.createPrettyPrint();
            //设置编码格式
            format.setEncoding("UTF-8");
            try {
                XMLWriter writer = new XMLWriter(new FileWriter(file),format);
                //写入数据
                writer.write(document);
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }


}
